package com.shuaiche.yeguan.views.photopicker;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;


import com.shuaiche.yeguan.R;
import com.shuaiche.yeguan.util.ToastShowUtils;

import java.util.ArrayList;

import androidx.viewpager.widget.ViewPager;

/**
 * Created by foamtrace on 2015/8/25.
 */
public class PhotoPreviewActivity extends Activity implements PhotoPagerAdapter.PhotoViewClickListener{

    public static final String EXTRA_PHOTOS = "extra_photos";
    public static final String EXTRA_CURRENT_ITEM = "extra_current_item";
    public static final String EXTRA_SHOW_DELETE = "extra_show_delete";

    public static final String EXTRA_SHOW_MAIN_PIC = "extra_show_main_pic";

    /** 选择结果，返回为 ArrayList&lt;String&gt; 图片路径集合  */
    public static final String EXTRA_RESULT = "preview_result";

    /** 预览请求状态码 */
    public static final int REQUEST_PREVIEW = 99;

    private ArrayList<String> paths;
    private ViewPagerFixed mViewPager;
    private PhotoPagerAdapter mPagerAdapter;
    private int currentItem = 0;
    private TextView tvTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.sc_photo_act_image_preview);

        initViews();

        paths = new ArrayList<>();
        ArrayList<String> pathArr = getIntent().getStringArrayListExtra(EXTRA_PHOTOS);
        if(pathArr != null){
            paths.addAll(pathArr);
        }

        currentItem = getIntent().getIntExtra(EXTRA_CURRENT_ITEM, 0);
        boolean bShowDelete = getIntent().getBooleanExtra(EXTRA_SHOW_DELETE, true);
        if(!bShowDelete){
            findViewById(R.id.ivDelete).setVisibility(View.GONE);
            findViewById(R.id.tvDelete).setVisibility(View.GONE);
        }

        boolean bShowMainPic = getIntent().getBooleanExtra(EXTRA_SHOW_MAIN_PIC, true);
        if (!bShowMainPic) {
            findViewById(R.id.tvMakeMainPic).setVisibility(View.GONE);
        }

        mPagerAdapter = new PhotoPagerAdapter(this, paths);
        mPagerAdapter.setPhotoViewClickListener(this);
        mViewPager.setAdapter(mPagerAdapter);
        mViewPager.setCurrentItem(currentItem);
        mViewPager.setOffscreenPageLimit(5);


        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                updateActionBarTitle();
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        updateActionBarTitle();
    }

    private void initViews(){
        mViewPager = (ViewPagerFixed) findViewById(R.id.vp_photos);
        tvTitle = (TextView)findViewById(R.id.tvTitle);
        findViewById(R.id.tvBack).setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        findViewById(R.id.tvDelete).setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                delete();
            }
        });
        findViewById(R.id.tvMakeMainPic).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                makeMainPic();
            }
        });

//        Toolbar mToolbar = (Toolbar) findViewById(R.id.pickerToolbar);
//        setSupportActionBar(mToolbar);
//        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public void OnPhotoTapListener(View view, float v, float v1) {
        onBackPressed();
    }

    public void updateActionBarTitle() {
        
        tvTitle.setText(getString(R.string.image_index, mViewPager.getCurrentItem() + 1, paths.size()));
//        getSupportActionBar().setTitle(
//                getString(R.string.image_index, mViewPager.getCurrentItem() + 1, paths.size()));
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent();
        intent.putExtra(EXTRA_RESULT, paths);
        setResult(RESULT_OK, intent);
        finish();
        super.onBackPressed();
    }

//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        getMenuInflater().inflate(R.menu.menu_preview, menu);
//        return true;
//    }
//
//    @Override
//    public boolean onOptionsItemSelected(MenuItem item) {
//
//        if(item.getItemId() == android.R.id.home){
//            onBackPressed();
//            return true;
//        }
//
//        // 删除当前照片
//        if(item.getItemId() == R.id.action_discard){
//            delete();
//        }
//        return super.onOptionsItemSelected(item);
//    }

    private void delete() {
        final int index = mViewPager.getCurrentItem();
        final String deletedPath =  paths.get(index);
//            Snackbar snackbar = Snackbar.make(getWindow().getDecorView().findViewById(android.R.id.content), R.string.deleted_a_photo,
//                    Snackbar.LENGTH_LONG);
        if(paths.size() <= 1){
            // 最后一张照片弹出删除提示
            // show confirm dialog
            new AlertDialog.Builder(this)
                    .setTitle(R.string.confirm_to_delete)
                    .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                        @Override public void onClick(DialogInterface dialogInterface, int i) {
                            dialogInterface.dismiss();
                            paths.remove(index);
                            onBackPressed();
                        }
                    })
                    .setNegativeButton(R.string.btn_cancel, new DialogInterface.OnClickListener() {
                        @Override public void onClick(DialogInterface dialogInterface, int i) {
                            dialogInterface.dismiss();
                        }
                    })
                    .show();
        }else{
//                snackbar.show();
            ToastShowUtils.showSuccessToast(getString(R.string.deleted_a_photo));
            paths.remove(index);
            mPagerAdapter.notifyDataSetChanged();
        }

//            snackbar.setAction(R.string.undo, new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    if (paths.size() > 0) {
//                        paths.add(index, deletedPath);
//                    } else {
//                        paths.add(deletedPath);
//                    }
//                    mPagerAdapter.notifyDataSetChanged();
//                    mViewPager.setCurrentItem(index, true);
//                }
//            });
    }


    private void makeMainPic() {
        final int index = mViewPager.getCurrentItem();
        String mainUrl = paths.get(index);
        paths.remove(index);
        paths.add(0, mainUrl);
        Intent intent = new Intent();
        intent.putExtra(EXTRA_RESULT, paths);
        setResult(RESULT_OK, intent);
        finish();
    }
}
